//
//  ExampleButtonView.swift
//  SwiftUIFly
//
//  Created by shen on 2023/10/18.
//

import SwiftUI
import AuthenticationServices

struct ExampleButtonView: View {
    @State private var showingAlert = false
    @State private var showingSheet = false
    @State private var showingActionSheet = false
    @State private var showButtonSheet = false

    var body: some View {
        Group {
            buttonGroup

            linkGroup

            navigationLinkGroup

            menuGroup

            #if os(iOS)
            SignInWithAppleButtonGroup
            #endif

            #if os(iOS)
            customButtonGroup
            #endif
        }
    }

    var customButtonGroup: some View {
        ExampleSection(
            title: "Custom Button Views",
            description: "Customize what buttons look like",
            content: {
                Button {
                    self.showButtonSheet = true
                } label: {
                    Text("Show Button Modifiers")
                }
                .sheet(isPresented: $showButtonSheet) {
                    ButtonModifiers().padding()
                }
            }
        )
    }

    var SignInWithAppleButtonGroup: some View {
        ExampleSection(
            title: "SignInWithAppleButton",
            description: "A control that you add to your interface to allow users to sign in with their Apple ID.",
            content: {
                    SignInWithAppleButton(
                        .signIn,
                        onRequest: { request in
                            request.requestedScopes = [.fullName, .email]
                        },
                        onCompletion: { _ in
                        }
                    )
            }
        )
    }

    var menuGroup: some View {
        ExampleSection(
            title: "Menu",
            description: "A control for presenting a contextually-appropriate menu of buttons.",
            content: {
                Group {
                    Menu("Show Menu") {
                        Button("Button") {}
                        Button("Button") {}
                        Menu("Submenu") {
                            Button("Button") {}
                            Button("Button") {}
                            Button("Button") {}
                        }
                        Divider()
                        Button("Button") {}
                        Menu("Submenu") {
                            Button("Button") {}
                            Button("Button") {}
                            Button("Button") {}
                        }
                    }

                    HStack {
                        #if os(iOS)
                        Text("Show Context Menu")
                        Spacer()
                        Text("Press & hold").italic().foregroundColor(.secondary)
                        #elseif os(macOS)
                        Text("Right click")
                        #endif
                    }
                    .contextMenu {
                        Button("Button") {}
                        Button("Button") {}
                        Button("Button") {}
                        Divider()
                        Button("Button") {}
                        Button("Button") {}
                    }
                }
            }
        )
    }

    var navigationLinkGroup: some View {
        ExampleSection(
            title: "NavigationLink",
            description: "A view that controls a navigation presentation.",
            content: {
                NavigationLink(destination: Text("Destination")) {
                    Text("Next")
                }
            }
        )
    }

    var linkGroup: some View {
        ExampleSection(
            title: "Link",
            description: "A control for navigating to a URL.",
            content: {
                Link("baidu.com", destination: URL(string: "https://baidu.com")!)
            }
        )
    }

    var buttonGroup: some View {
        ExampleSection(
            title: "Button",
            description: "A control that performs an action when triggered.",
            content: {
                Group {
                    Button {
                        self.showingAlert = true
                    } label: {
                        Text("Show Alert")
                    }
                    .alert(isPresented: $showingAlert) {
                        Alert(
                            title: Text("Title"),
                            message: Text("Message"),
                            primaryButton: .default(Text("Confirm")),
                            secondaryButton: .cancel()
                        )
                    }

                    Button {
                        self.showingSheet = true
                    } label: {
                        Text("Show Sheet")
                    }
                    .sheet(isPresented: $showingSheet) {
                        Text("Sheet").padding()
                        #if os(macOS)
                        Button("Close") {
                            showingSheet.toggle()
                        }.padding()
                        #endif
                    }

                    #if !os(OSX)
                    Button {
                        self.showingActionSheet = true
                    } label: {
                        Text("Show Action Sheet")
                    }
                    .actionSheet(isPresented: $showingActionSheet) {
                        ActionSheet(title: Text("Title"), message: Text("Message"), buttons: [
                            .destructive(Text("Delete")),
                            .default(Text("Option 1")) {},
                            .default(Text("Option 2")) {},
                            .cancel()
                        ])
                    }
                    #endif
                }
            }
        )
    }
}

#Preview {
    ExampleButtonView()
}
